package com.zn.net.socket
{
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	public class SocketPackageOut extends ByteArray
	{
		public function SocketPackageOut(code:int,toId:int = 0,extend1:int = 0,extend2:int = 0)
		{
			super();
		//	this.endian = Endian.BIG_ENDIAN;
			this.writeShort(0);//长度
			this.writeShort(0);//效验位
			this.writeShort(code);
			this.writeInt(toId);
			this.writeInt(extend1);
			this.writeInt(extend2);
		}
		
		private var _code:int;
		
		public function get code():int
		{
			return _code;
		}
		
		public function pack():void
		{
			var checksum:int = calculateCheckSum();
			var temp:ByteArray = new ByteArray();
			temp.writeShort(this.length);
			temp.writeShort(checksum);
			this[0] = temp[0];
			this[1] = temp[1];
			this[2] = temp[2];
			this[3] = temp[3];
		}
		
		public function calculateCheckSum():int
		{
			var val1:int = 0x77;
			var i:int = 6;
			var len:int = this.length;
			
			while (i < len)
			{
				val1 += this[i++];
			}
			
			return (val1) & 0x7F7F;
		}
		
	}
}